import {
    login,
    logout,
    getInfo
} from '@/api/login'

const user = {
    state: {
        token: uni.getStorageSync('token') || '',
        mobile: uni.getStorageSync('mobile') || {},
        setInfo: uni.getStorageSync('userInfo') || {},
        receiveMsg: {},
        receivePush: {}, //消息推送
        isToLogin: false, //是否前往登录
        isPush: false, //是否消息推送弹窗
        currentTabbar: 0,
        unreadMessages: 0, //我的页面未读消息数
        chatList: [], //聊天列表
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_MOBILE: (state, mobile) => {
            state.mobile = mobile
        },
        SET_INFO: (state, setInfo) => {
            state.setInfo = setInfo
        },
        SET_ISToLOGIN: (state, isToLogin) => {
            console.log(123)
            state.isToLogin = isToLogin
        },
        SET_ISPUSH: (state, isPush) => {
            state.isPush = isPush
        },
        SET_CURRENTTABBAR: (state, currentTabbar) => {
            state.currentTabbar = currentTabbar
        },
        SET_CHATLIST: (state, chatList) => {
            chatList.forEach(e => {
                e.tisNum = 0
            });
            state.chatList = chatList
        },
        UPDATE_CHATLIST: (state, chatList) => {
            if (state.chatList.length > 0) {
                let index = state.chatList.findIndex(item => item.uid == chatList.content.formatuid);
                if (index !== -1) {
                    state.chatList[index].tisNum++
                } else {
                    state.chatList.push({
                        ...chatList,
                        tisNum: 1
                    });
                }
            } else {
                state.chatList.push({
                    ...chatList,
                    tisNum: 1
                });
            }
        },
        CLEAR_TISNUM: (state, format_uid) => {
            let index = state.chatList.findIndex(item => item.format_uid == format_uid);
            if (index !== -1) {
                state.chatList[index].tisNum = 0
            }
            console.log(state.chatList[index].tisNum, "state.chatList[index].tisNum")
        },
        updateReceiveMsg(state, data) {
            state.receiveMsg = data
            // state.chatList.forEach(e => {
            //     e.tisNum+
            // });
            state.unreadMessages++
        },
        updateReceivePush(state, data) {
            state.receivePush = data
        },

        CLEAR_UNREAD_MESSAGES: (state, unreadMessages) => {
            state.unreadMessages = 0
        },
    },

    actions: {
        // 账号密码登录
        Login({
            commit
        }, userInfo) {
            userInfo.login_account = userInfo.login_account.trim()
            return new Promise((resolve, reject) => {
                console.log(11111)
                login(userInfo).then(res => {
                    console.log(22222)
                    if (res.code == 1) {
                        console.log(res, 777)
                        uni.setStorageSync('token', res.data.token)
                        uni.setStorageSync('userInfo', JSON.stringify(res.data))
                        commit('SET_TOKEN', res.data.token)
                        uni.setStorageSync('mobile', userInfo.login_account)
                        commit('SET_MOBILE', userInfo.login_account)
                        resolve(res)
                    }
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 获取用户信息
        GetInfo({
            commit,
            state
        }) {
            return new Promise((resolve, reject) => {
                getInfo({
                    token: state.token,
                    reqtype: uni.getStorageSync('platform') == 'ios' ? 'IOS' : 'Android'
                }).then(res => {
                    const user = res.data;
                    // user.imagePath = user.imagePath == "" ? require("@/static/mine/logo.png") : process.env.VUE_APP_BASE_API +
                    // 	user.imagePath;
                    uni.setStorageSync('userInfo', user)
                    commit('SET_INFO', user)
                    resolve(res.data)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 退出系统
        LogOut({
            commit,
            state
        }) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    uni.setStorageSync('token', '')
                    commit('SET_TOKEN', '')
                    uni.setStorageSync('userInfo', '')
                    commit('SET_INFO', '')
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 前端 登出
        FedLogOut({
            commit
        }) {
            return new Promise(resolve => {
                uni.removeStorageSync("token");
                uni.removeStorageSync("mobile");
                uni.clearStorage();
                resolve()
            })
        }
    }
}

export default user